package com.cictec.middleware.gps.engine;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.cictec.middleware.commons.utils.Constants;
import com.cictec.middleware.core.entity.Bus;
import com.cictec.middleware.core.entity.Device;
import com.cictec.middleware.core.entity.WarnMsg;

public class BusInstance extends AbstractBusInstance {

	private static final long serialVersionUID = -3636601847395266928L;
	
	
	public BusInstance(Bus bus, Device device) {
		this.bus = bus;
		this.device = device;
	}

	/**
	 * 车辆的静态属性，从数据库车辆表加载。
	 */
	private Bus bus;

	/**
	 * 调度主机的设备属性，从数据库设备表加载。
	 */
	private Device device;


	/**
	 * 车辆实例的时序表，时序以车辆的gps位置为基准。
	 */
	private List<TimingPoint> timingSequential = Collections.synchronizedList(new LinkedList<TimingPoint>());


	/** 当前报警信息 */
	private Map<String, WarnMsg> warns = new ConcurrentHashMap<String, WarnMsg>();



	/**
	 * 车辆设备是否在线。true - 设备连接正常 ; false - 设备连接掉线
	 */
	private volatile boolean devOnline = false;
	
	public boolean isDevOnline() {
		return devOnline;
	}

	public void setDevOnline(boolean devOnline) {
		this.devOnline = devOnline;
		setLoginStatus(devOnline ? Constants.VAL_LOGIN_ONLINE : Constants.VAL_LOGIN_OFFLINE);
	}
	



	public Bus getBus() {
		return bus;
	}

	public void setBus(Bus bus) {
		this.bus = bus;
	}

	public Device getDevice() {
		return device;
	}

	public void setDevice(Device device) {
		this.device = device;
	}


	public List<TimingPoint> getTimingSequential() {
		return timingSequential;
	}

	public void setTimingSequential(List<TimingPoint> timingSequential) {
		this.timingSequential = timingSequential;
	}
	
	/**
	 * 获取 时序点信息,从时序节点数组中取，数组以按照时间排序
	 * @param index  index<0, 0表示当前点，-1表示前一个点的...
	 * @return
	 */
	public TimingPoint getTimingPoint(int index){
		if(Math.abs(index) < timingSequential.size()){
			return timingSequential.get(getTimingSequential().size() + (index-1));
		}else{
			return null;
		}
	}
	
	/**
	 * 【添加】车辆实例的时序表，时序以车辆的gps位置为基准（按照时间顺序已经排号顺序了
	 * @param timingPoint
	 */
	public synchronized void addTimingPoint(TimingPoint timingPoint) {
		getTimingSequential().add(timingPoint);
	}
	
	/**
	 * 移除最后一个位置信息
	 */
	public synchronized void removeLastTimingPoint() {
		getTimingSequential().remove(getTimingSequential().size() -1);
	}

	public Map<String, WarnMsg> getWarns() {
		return warns;
	}

	public void setWarns(Map<String, WarnMsg> warns) {
		this.warns = warns;
	}

	public synchronized void setWarns(String warnType, WarnMsg warnMsg) {
		if (warnMsg == null) {
			warns.remove(warnType);
		} else {
			this.warns.putIfAbsent(warnType, warnMsg);
		}
	}
	
	public WarnMsg getWarnsByWarnType(String warnType) {
		return warns.get(warnType);
	}

	/**
	 * 是否里程计算
	 * @return
	 */
	public boolean isBusMileageCaculate(){
		return getAttributes().containsKey(Constants.CALCULATE_MILEAGE);
	}
}
